import argparse
import re

# 统计字符数
def count_characters(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        return len(text)
    
# 统计单词数
def count_words(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        words = re.findall(r'\b\w+\b', text)        # 使用正则表达式提取单词
        return len(words)
    
# 统计句数
def count_sentences(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        text = file.read()
        sentences = re.split(r'[\.\?\!]\s', text)   # 使用正则表达式提取句子
        return len(sentences)
    
def main():
    # 命令行帮助描述
    parser = argparse.ArgumentParser(description='Text and Code Counting Program')
    parser.add_argument('file_path', help='File path')
    parser.add_argument('-c', action='store_true', help='Count characters')
    parser.add_argument('-w', action='store_true', help='Count words')
    parser.add_argument('-s', action='store_true', help='Count sentences')
    
    args = parser.parse_args()

    # 命令行结果输出
    if args.c:      # -c 字符
        count = count_characters(args.file_path)
        print(f"Characters count: {count}")
    
    if args.w:      # -w 单词
        count = count_words(args.file_path)
        print(f"Word count: {count}")
    
    if args.s:      # -s 句子
        count = count_sentences(args.file_path)
        print(f"Sentence count: {count}")
        
if __name__ == '__main__':
    main()
    